#!/bin/sh
##
## This file is part of the sigrok-util project.
##
## Copyright (C) 2016-2018 Uwe Hermann <uwe@hermann-uwe.de>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##

set -e

# The base path prefix where the compiled packages will be installed.
PREFIXBASE=$HOME/sr_msys2

# The base path prefix where to download files to and where to build packages.
BUILDBASE=./build

# The $PREFIX subdir where the libsigrok Python bindings will be installed.
PYBASE=lib/python2.7/site-packages

# Edit this to control verbose build output.
# V="V=1 VERBOSE=1"

# Edit this to enable/disable/modify parallel compiles.
PARALLEL="-j 2"

# Edit this to enable/disable debug builds.
DEBUG=0

# You usually don't need to change anything below this line.

# -----------------------------------------------------------------------------

# Abort if the user runs the script in an "MSYS2 MSYS" shell.
if [ $MSYSTEM = "MSYS" ]; then
	echo "Building in an 'MSYS2 MSYS' shell will not work, aborting."
	echo "Please use the 'MSYS2 MinGW 64-bit' (or 32-bit) shell."
	exit
fi

# The build target type: "i686" (32bit) or "x86_64" (64bit).
# This is selected based on which MSYS2 shell is being used to do the build.
TARGET=$MSYSTEM_CARCH

VER_SIGROK_FIRMWARE_FX2LAFW=0.1.6

WGET="wget -c --quiet"
GIT_CLONE="git clone --depth=1"

REPO_BASE="git://sigrok.org"

# Construct the build and install directory pathnames.
if [ $TARGET = "i686" ]; then
	SUFFIX="32"
else
	SUFFIX="64"
fi
if [ $DEBUG = 1 ]; then
	# CFLAGS/CXXFLAGS contains "-g" per default for autotools projects.
	BUILD_TYPE="Debug"
	PREFIX=$PREFIXBASE"_debug_"$SUFFIX
	BUILDDIR=$BUILDBASE"_debug_"$SUFFIX
else
	BUILD_TYPE="Release"
	PREFIX=$PREFIXBASE"_release_"$SUFFIX
	BUILDDIR=$BUILDBASE"_release_"$SUFFIX
fi

PYPATH=$PREFIX/$PYBASE

# Eliminate the host Python path environment variable, if any is set.
export PYTHONPATH=

# -----------------------------------------------------------------------------

P="$PREFIX/lib/pkgconfig:$PKG_CONFIG_PATH"
C="$C --prefix=$PREFIX"
L="--disable-shared --enable-static"

# Path to Qt binaries (needed for cmake to find the Qt libs).
PATH="/mingw32/qt5-static/bin:$PATH"

W="mingw-w64-$TARGET"

mkdir -p $PYPATH

# Remove build directory contents (if any) and create a new build dir.
rm -rf $BUILDDIR
mkdir $BUILDDIR
cd $BUILDDIR

# -----------------------------------------------------------------------------

if [ "x$1" = "xprepare" ]; then
  # Update package list, install/update all requirements.
  pacman -Sy
  pacman -S \
	autoconf automake autoconf-archive libtool make pkg-config wget patch \
	$W-toolchain $W-glib2 $W-glibmm $W-libusb $W-libftdi $W-check $W-boost \
	$W-libzip $W-doxygen $W-python3 $W-python3-numpy $W-python3-gobject \
	$W-python3-setuptools $W-swig $W-qt5-static $W-cmake $W-nsis \
	$W-hidapi
  exit
fi

# -----------------------------------------------------------------------------

# libusb
$GIT_CLONE git://github.com/dickens/libusb -b event-abstraction-v4
cd libusb
./bootstrap.sh
./configure $C $L
make -j1 $V
make install $V
cd ..

# libserialport
$GIT_CLONE $REPO_BASE/libserialport
cd libserialport
./autogen.sh
mkdir build
cd build
../configure $C $L
$SB make $PARALLEL $V
make install $V
cd ../..

# libsigrok
$GIT_CLONE $REPO_BASE/libsigrok
cd libsigrok
./autogen.sh
mkdir build
cd build
PKG_CONFIG_PATH=$P PYTHON=python3 ../configure $C $L --disable-python
$SB make $PARALLEL $V
PYTHONPATH=$PYPATH $SB make install $V
# $SB make check $V # TODO
cd ../..

# libsigrokdecode
$GIT_CLONE $REPO_BASE/libsigrokdecode
cd libsigrokdecode
./autogen.sh
mkdir build
cd build
PKG_CONFIG_PATH=$P ../configure $C $L
$SB make $PARALLEL $V
make install $V
# $SB make check $V # TODO
cd ../..

# sigrok-firmware
$GIT_CLONE $REPO_BASE/sigrok-firmware
cd sigrok-firmware
./autogen.sh
mkdir build
cd build
# Nothing gets cross-compiled here, we just need 'make install' basically.
../configure $C
make install $V
cd ../..

# sigrok-firmware-fx2lafw
$WGET http://sigrok.org/download/binary/sigrok-firmware-fx2lafw/sigrok-firmware-fx2lafw-bin-$VER_SIGROK_FIRMWARE_FX2LAFW.tar.gz
tar xfz sigrok-firmware-fx2lafw-bin-$VER_SIGROK_FIRMWARE_FX2LAFW.tar.gz
cd sigrok-firmware-fx2lafw-bin-$VER_SIGROK_FIRMWARE_FX2LAFW
cp *.fw $PREFIX/share/sigrok-firmware/
cd ..

# TODO: Doesn't build, libsigrokdecode linking errors.
# # sigrok-cli
# $GIT_CLONE $REPO_BASE/sigrok-cli
# cd sigrok-cli
# ./autogen.sh
# mkdir build
# cd build
# PKG_CONFIG_PATH=$P ../configure $C $L
# $SB make $PARALLEL $V
# make install $V
# # makensis -DHOME=$HOME contrib/sigrok-cli_cross.nsi # TODO
# cd ../..

# TODO: Doesn't build, various linking errors.
# PulseView
$GIT_CLONE $REPO_BASE/pulseview
cd pulseview
mkdir build
cd build
PKG_CONFIG_PATH=$P $SB cmake \
	-G "Unix Makefiles" \
	-DCMAKE_INSTALL_PREFIX:PATH=$PREFIX \
	-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
	-DDISABLE_WERROR=y \
	-DENABLE_TESTS=n \
	..
$SB make $PARALLEL $V
make install $V
# $SB make test $V # TODO
# makensis -DHOME=$HOME contrib/pulseview_cross.nsi # TODO
cd ../..
